package www.golf.web.util;

import java.util.Iterator;
import java.util.Map;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.event.ListDataEvent;
import org.zkoss.zul.event.ListDataListener;

public class UtilEvent {
	/**
	 * 設定model 資料change事件
	 * @author angle
	 * @param listModel 
	 * @param setModelEvent 介面setModelEvent必須實作內容
	 */
	public void addEventForModel(ListModel listModel,final SetModelEvent setModelEvent){
		listModel.addListDataListener( new ListDataListener(){
			public void onChange(ListDataEvent event) {
				setModelEvent.Content(event);
			}
		});
	}
	/**
	 * 加入事件
	 * @author angle
	 * @param component 框架中最上層元素
	 * @param attribute 
	 */
	public void addEvent(Component component,final Map attribute){
		Iterator<String> it =attribute.keySet().iterator();
		while(it.hasNext()){
			final String key = (String) it.next(); 
			String keyid=key.substring(0,key.indexOf("[", 0));
			String keyevent=key.substring(key.indexOf("[", 0)+1,key.indexOf("]", 0));
			component.getFellow(keyid).addEventListener(keyevent, new EventListener() {
				public void onEvent(org.zkoss.zk.ui.event.Event event)
				throws Exception {
					((SetEvent) attribute.get(key)).Content();
				}	
			});
		}
	}
	public interface SetEvent{
		public void Content();
	}
	public interface SetModelEvent{
		public void Content(ListDataEvent event);
	}	
}
